package com.zheng.common.util;

import com.zheng.common.base.PublishDto;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Mq推送工具类
 * Created by shuzheng on 2018/6/28.
 */
public class MqMessageUtil {

    //实现监听mqtt客户端是否在线
    //"$SYS/brokers/+/clients/+/connected";//客户端上线主题 所有节点的 所有设备
    //"$SYS/brokers/+/clients/+/disconnected";//客户端下线主题
    //订阅以上两个主题即可

    private static final Logger LOGGER = LoggerFactory.getLogger(MqMessageUtil.class);
    private static final String CLIENTID="BJS_SERVER";

    public static String caFilePath = "D:/for-iot/LURA/src/mytest/samplecerts/ca.crt";
    public static String clientCrtFilePath = "D:/for-iot/LURA/src/mytest/samplecerts/client.crt";
    public static String clientKeyFilePath = "D:/for-iot/LURA/src/mytest/samplecerts/client.key";
    /**
     * 推送消息
     */
    public void publish(String host,String userName,String passWord,PublishDto publicDto,MqttCallback callback) throws Exception {
        //MemoryPersistence设置clientid的保存形式，默认为以内存保存
        MqttClient client = new MqttClient(host, CLIENTID, new MemoryPersistence());
        //与activeMQ连接的方法
        // new mqttConnection 用来设置一些连接的属性
        MqttConnectOptions options = new MqttConnectOptions();
        // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录，这里设置为true表示每次连接到服务器都以新的身份连接
        // 换而言之，设置为false时可以客户端可以接受离线消息
        options.setCleanSession(false);
        // 设置连接的用户名和密码
        options.setUserName(userName);
        options.setPassword(passWord.toCharArray());
        // 设置超时时间
        options.setConnectionTimeout(10);
        // 设置会话心跳时间
        options.setKeepAliveInterval(20);
        // TLS连接配置
        //options.setSocketFactory(
        //        SslUtil.getSocketFactory(caFilePath, clientCrtFilePath, clientKeyFilePath, "cs123456"));
        // 设置回调类
        client.setCallback(callback);
        // 连接
        client.connect(options);

        //new mqttMessage
        MqttMessage message = new MqttMessage();
        //设置服务质量,0:最多一次，1:至少一次
        message.setQos(0);
        //设置是否在服务器中保存消息体
        message.setRetained(true);
        //设置消息的内容
        message.setPayload(publicDto.getPayload().toString().getBytes());
        //发布
        //publish(topic, message);
    }

    public void publishTopic(MqttTopic topic, MqttMessage message) throws MqttPersistenceException,  MqttException {
        // 发布的方法
        // new mqttDeliveryToken
        MqttDeliveryToken token = topic.publish(message);
        // 发布
        token.waitForCompletion();
    }

}
